<?php
declare (strict_types = 1);

namespace app\api\model;
use app\api\model\Student as ModelStudent;
use app\api\model\City as ModelCity;
use app\api\model\Course as ModelCourse;
use app\api\model\StudentChapterRelation as ModelStudentChapterRelation;
use app\api\model\TeachStudentRelation as ModelTeachStudentRelation;
use think\Db as ThinkDb;
use think\facade\Db;
use app\api\model\Common as ModelCommon;

/**
 * 课程模型
 */
class StudentManagement extends Base
{
    protected $autoWriteTimestamp = 'datetime';
    /**
     * 
     * 获取学员信息
     */
    public static function getAllStatus($page=1,$size=8)
    {
        $student = ModelStudent::field('id,truename,course_id,account,progress,gender,birthday,referrer,source_id,city_code,province_code,semester_id,apply_time');
        $count = count($student->select());
        $start = ($page - 1) * $size;
        $data = $student->order('id','desc')->limit($start,$size)->select();
        if(empty($data)){
            return [
                'code' => 200,
                'message' => '查询成功',
                'data' => $data
            ];
        } 
       
        ModelCommon::encryptPhone($data);
        return [
            'code' => 200,
            'message' => '数据获取成功',
            'data' => $data,
            'count' => $count
        ];
    } 

    /**
     * 
     * 获取学生详细信息
     */
    public static function getStudentInfo($id)
    {
        $student = ModelStudent::field('id,truename,course_id,account,progress,gender,birthday,referrer,source_id,city_code,province_code,semester_id,apply_time')->find($id);
        $data = ModelCity::getAllCity($student);
        $student['citys'] = $data['data']->toArray();
        return [
            'code' => 200,
            'message' => '数据获取成功',
            'data' => $student
        ];
    }

    /**
     * 新增学员信息
     */
    public static function addStudent($data)
    {
        $student = ModelStudent::where('account',$data['account'])->find();
        
        if(! empty($student)){
            return [
                'code' => 400,
                'message' => '手机号已注册'
            ];
        }

        try{
            ModelStudent::create($data); 
            return [
                'code' => 200,
                'message' => '新增成功'
            ];
        } catch (\Exception $e) {
            return [
                'code' => 400,
                'message' => '信息输入错误'
            ];
        }

    }

    /**
     * 
     * 删除学员信息
     */
    public static function delStudent($id)
    {
        // 启动事务
        Db::startTrans();
        try{
            $stuChaRel = ModelStudentChapterRelation::where('student_id',$id)->select();
            $teaStuRel = ModelTeachStudentRelation::where('student_id',$id)->select();
            if(! empty($stuChaRel)){
                $stuChaRel->delete();
            }
            if(! empty($teaStuRel)){
                $teaStuRel->delete();
            }
            ModelStudent::destroy($id);
            Db::commit();
            return [
                'code' => 200,
                'message' => '删除成功',
            ];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return [
                'code' => 4024,
                'message' => $e,
            ];
        }

    }

    /**
     * 
     * 编辑学员信息
     */
   
    public static function updateStudent($data)
    {
        $student = ModelStudent::where('account',$data['account'])->find();
        if(! empty($student)){
            if($student->id != $data['id']){
                return [
                    'code' => 400,
                    'message' => '手机号已注册'
                ];
            }  
        }
        $stu = ModelStudent::find($data['id']);  
        if(empty($stu)){
            return [
                'code' => 404,
                'message' => '学生信息不存在'
            ];
        }
        $stu->account = $data['account'];
        $stu->truename = $data['truename'];
        $stu->course_id = $data['course_id'];
        $stu->gender = $data['gender'];
        $stu->birthday = $data['birthday'];
        $stu->referrer = $data['referrer'];
        $stu->source_id = $data['source_id'];
        $stu->province_code = $data['province_code'];
        $stu->city_code = $data['city_code'];
        $stu->semester_id = $data['semester_id']; 
        $stu->apply_time = $data['apply_time'];
        $stu->save();
        return [
            'code' => 200,
            'message' => '修改成功'
        ];
    }

    /**
     * 
     * 搜索学员信息
     */

    public static function searchStudent($data)
    {
        $where = [];
        if(! empty($data['name'])){
            $where[] = ['truename','like','%' . $data['name'] . '%'];
        }
        if(! empty($data['city_code'])){
            $where[] = ['city_code','=',$data['city_code']];
        }else{
            if(! empty($data['province_code'])){
                $where[] = ['province_code','=',$data['province_code']];
            }
        }
  
        $start = ($data['page'] - 1) * $data['size'];   
        $stuInfo = ModelStudent::where($where)->field('id,truename,course_id,account,progress,gender,birthday,referrer,source_id,city_code,province_code,semester_id,apply_time');
        $count = count($stuInfo->select());
        $data = $stuInfo->order('create_time','desc')->limit($start,$data['size']*1)->select();
        ModelCommon::encryptPhone($data->toArray());
        if(empty($data)){
            return [
                'code' => 400,
                'message' => '暂无该学生信息'
            ];
        }else{
            return [
                'code' => 200,
                'message' => '搜索成功',
                'data' => $data,
                'count' => $count
            ];
        }
    }



}